package com.zjxny.jobs;

import java.util.Date;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;

import cn.hutool.core.convert.Convert;
import cn.hutool.core.date.DatePattern;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import lombok.extern.slf4j.Slf4j;
import xyz.erupt.core.annotation.EruptHandlerNaming;
import xyz.erupt.job.handler.EruptJobHandler;


@Slf4j
@Service
@EruptHandlerNaming("车辆年检检查")
public class CarCheckUpJob implements EruptJobHandler{

	@Resource
	JdbcTemplate jdbcTemplate;
	
	@Override
	public String exec(String code, String param) {
		log.info("CarCheckUpJob.exec");
		
		Date ctime = new Date();
		if(StrUtil.isNotBlank(param)) {
			ctime = DateUtil.parse(param);
		}
		
		//D60
		String dOne = DateUtil.format(DateUtil.offsetDay(ctime, -60), DatePattern.NORM_DATE_PATTERN);
		String sqlOne = "select id,hphm,customer_info_id customerId,account_info_id accountId,yxqz from car_info where yxqz='"+dOne+"'";
		List<Map<String,Object>> listOne = jdbcTemplate.queryForList(sqlOne);
		if(!listOne.isEmpty()) {
			generateSms(listOne,4);//生成短信内容
		}
		
		//D30
		String dTwo = DateUtil.format(DateUtil.offsetDay(ctime, -30), DatePattern.NORM_DATE_PATTERN);
		String sqlTwo = "select id,hphm,customer_info_id customerId,account_info_id accountId,yxqz from car_info where yxqz='"+dTwo+"'";
		List<Map<String,Object>> listTwo = jdbcTemplate.queryForList(sqlTwo);
		if(!listTwo.isEmpty()) {
			generateSms(listTwo,5);//生成短信内容
		}

		//D15
		String dThird = DateUtil.format(DateUtil.offsetDay(ctime, -15), DatePattern.NORM_DATE_PATTERN);
		String sqlThird = "select id,hphm,customer_info_id customerId,account_info_id accountId,yxqz from car_info where yxqz='"+dThird+"'";
		List<Map<String,Object>> listThird = jdbcTemplate.queryForList(sqlThird);
		if(!listThird.isEmpty()) {
			generateSms(listThird,6);//生成短信内容
		}
		return null;
	}
	
	private void generateSms(List<Map<String,Object>> list, Integer type) {
		Iterator<Map<String,Object>> iter = list.iterator();
		String phoneno = "4000011290";
		while(iter.hasNext()) {
			Map<String,Object> data = iter.next();
			String hphm =(String) data.get("hphm");
			Date yxqzDate = (Date) data.get("yxqz");
			
			String yxqz = DateUtil.format(yxqzDate, DatePattern.NORM_DATE_PATTERN);
			Long accountId = Convert.toLong(data.get("accountId"));
//			Map<String,Object> accountInfo = getAcccountInfo(accountId);
			//有可能会出现空值
			Long customerId = 0L;
			if(ObjectUtil.isNotNull(data.get("customerId"))) {				
				customerId = Convert.toLong(data.get("customerId"));
				//获取客户资料
//				Map<String,Object> customer = getCustomerInfo(customerId);
			}
			String content = "";
			if(type==4) {//D60
				content = String.format("您的车牌%s检验有效期至%s,请按时办理定期检验业务。按规定逾期未检验车辆不得上路行驶。有疑问请咨询。有疑问请咨询：%s",hphm,yxqz, phoneno);
			} else if(type==5) {//D30
				content = String.format("您的车牌%s检验有效期至%s，请按时办理定期检验业务。有疑问请咨询：%s",hphm,yxqz, phoneno);
			} else if(type==6){//D15
				content = String.format("您的车牌%s检验有效期至%s，按规定逾期未检验车辆不得上路行驶。我司将有权力追究其产生的所有损失。若已检车，请忽略本条信息",hphm,yxqz);
			}
			
			String iSql = "";
			if(customerId>0) {
				String iSqlPrefix = "insert into sms_info(create_time,update_time,status,sms_tpl_info_id,hphm,content,type,account_info_id,customer_info_id) values(now(),now(),0";
				iSql = String.format("%s,%d,'%s','%s',%d,%d,%d)",iSqlPrefix,type, hphm,content,type,accountId,customerId);
			} else {
				String iSqlPrefix = "insert into sms_info(create_time,update_time,status,sms_tpl_info_id,hphm,content,type,account_info_id) values(now(),now(),0";
				iSql = String.format("%s,%d,'%s','%s','%d','%d')",iSqlPrefix,type, hphm,content,type,accountId);
			}
			int affectNum = jdbcTemplate.update(iSql);
			log.info("hphm={},affectNum={},sql={}",hphm,affectNum,iSql);
		}
	}
	
	private Map<String,Object> getCustomerInfo(Long id){
		String sql = "select * from customer_info where id='"+id+"'";
		try {
			return jdbcTemplate.queryForMap(sql);
		} catch (Exception e) {
		}
		return null;
	}
	
	private Map<String,Object> getAcccountInfo(Long id){
		String sql = "select * from account_info where id='"+id+"'";
		try {
			return jdbcTemplate.queryForMap(sql);
		} catch (Exception e) {
		}
		return null;
	}
	
	public static void main(String[] args) {
		Date ctime = new Date();
		String dOne = DateUtil.format(DateUtil.offsetDay(ctime, -15), DatePattern.NORM_DATE_PATTERN);
		System.err.println(dOne);
	}
}
